package com.caigou.modules.personal.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.personal.bean.BoardBean
import com.caigou.modules.personal.bean.BoardItemBean
import com.caigou.modules.personal.bean.BoardReplyBean
import com.caigou.modules.personal.repository.PersonalRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/03
 */
@HiltViewModel
class BoardViewModel @Inject constructor(private val repository: PersonalRepository) : ViewModel() {

    val boardListLiveData = MutableLiveData<Resource<BoardBean>>()
    val boardReplyLiveData = MutableLiveData<Resource<BoardReplyBean>>()
    val boardDetailLiveData = MutableLiveData<Resource<BoardItemBean>>()
    val boardDetailReplyLiveData = MutableLiveData<Resource<BoardReplyBean>>()

    fun getBoardList(page: Int, limit: Int, boardUserId: String, currentUserId: String) {
        boardListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getBoardList(page, limit, boardUserId, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<BoardBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    boardListLiveData.value = it
                }
        }
    }

    fun setBoardReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, boardUserId: String) {
        boardReplyLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setBoardReply(content, files, senderId, receiverId, boardUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<BoardReplyBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    boardReplyLiveData.value = it
                }
        }
    }

    fun getBoardDetail(boardUserId: String, replyId: Int) {
        boardDetailLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getBoardDetail(boardUserId, replyId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<BoardItemBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    boardDetailLiveData.value = it
                }
        }
    }

    fun setBoardDetailReply(content: String, files: MutableList<File>, parentId: Int, senderId: String, receiverId: String, boardUserId: String) {
        boardDetailReplyLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setBoardDetailReply(content, files, parentId, senderId, receiverId, boardUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<BoardReplyBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    boardDetailReplyLiveData.value = it
                }
        }
    }
}